package cn.jsu.oj.dispatchServer.satellite.handler;

import cn.jsu.oj.dispatchServer.satellite.annotation.HandlerType;
import cn.jsu.oj.dispatchServer.satellite.factory.Factory;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.PackNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.RequestNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.ResponseNet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;

/**
 * 关机处理器
 *
 * @author shan
 * @date 2022/07/05
 */
@Slf4j
@Component("stopHandler")
@HandlerType(typeNo = 6)
public class StopHandler implements Handler {

    @Autowired
    Factory handlerFactory;

    /**
     * 服务器关闭处理
     *
     * @param packNet net包
     */
    @Override
    public void handle(PackNet<?> packNet) throws FileNotFoundException {
        String uuid = ((RequestNet) packNet.getMessage()).getUuid();
        ((Handler) handlerFactory.getProduct((short) 4))
                .handle(new PackNet<>(packNet.getChannel(),
                        new ResponseNet(uuid, true, (short) 6)));
        log.debug("----------即将关闭，" + "指令号: " + uuid + "......");
        System.exit(0);
    }
}
